package com.lanou.projecta_163news.Welcome;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.lanou.projecta_163news.R;

import java.util.ArrayList;


/**
 * Created by dllo on 16/3/4.
 */
public class WelcomeActivity extends AppCompatActivity implements View.OnClickListener {
    private SharedPreferences sp;
    private Handler handler;
    private boolean isLife = true;
    private ViewPager welcomeVp;
    private TabLayout welcomeTl;
    private ArrayList<Fragment> welcomeData;
    private WelcomeAdapter welcomeAdapter;

    private Button jumpBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        jumpBtn = (Button) findViewById(R.id.welcome_jump_btn);
        jumpBtn.setOnClickListener(this);

        initView();
        firstStart();
    }

    //
    //第一次开启软件
    //开启欢迎页
    private void firstStart() {
        sp = getSharedPreferences("welcome", MODE_PRIVATE);
        if (sp.getBoolean("isFirst", true)) {
            handler = new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(Message msg) {
                    switch (msg.what) {
                        case 1:
                            jumpBtn.setText("跳过(" + String.valueOf(msg.arg1) + ")");
                            break;
                        case 2:
                            jump();
                            break;
                    }
                    return false;
                }
            });
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 8; i > 0; i--) {

                        Message msg = new Message();
                        msg.arg1 = i;
                        msg.what = 1;
                        handler.sendMessage(msg);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if (isLife) {
                        handler.sendEmptyMessage(2);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putBoolean("isFirst", false);
                        editor.commit();
                    }
                }
            }).start();
        } else {
            jump();
        }
    }

    private void initView() {

        welcomeVp = (ViewPager) findViewById(R.id.welcome_vp);
        welcomeTl = (TabLayout) findViewById(R.id.welcome_tablayout);
        welcomeData = new ArrayList<>();
        welcomeData.add(new WelcomeOneFragment());
        welcomeData.add(new WelcomeTwoFragment());
        welcomeData.add(new WelcomeThreeFragment());
        welcomeAdapter = new WelcomeAdapter(getSupportFragmentManager(), welcomeData);
        welcomeVp.setAdapter(welcomeAdapter);
        welcomeTl.setTabTextColors(Color.BLACK, Color.YELLOW);
        welcomeTl.setupWithViewPager(welcomeVp);

    }

    private void jump() {
        Intent intent = new Intent(this, GuideActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        isLife = false;
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        jump();
    }
}
